package repository

import (
	"gin_server/internal/model"   // 请替换为实际的包路径
	"gin_server/internal/request" // 请替换为实际的包路径

	"gorm.io/gorm"
)

// UserRepository 定义user仓库接口
type UserRepository interface {
	// Create 创建新user
	Create(entity *model.User) (*model.User, error)

	// FindByID 根据ID获取user
	FindByID(id uint) (*model.User, error)

	// FindAll 获取所有user列表
	FindAll(params *request.UserQueryRequest) ([]*model.User, int64, error)

	// Update 更新user信息
	Update(id uint, entity *request.UserUpdateRequest) error

	// Delete 软删除user
	Delete(id uint) error

	// Restore 恢复user
	Restore(id uint) error

	// ForceDelete 真删除user
	ForceDelete(id uint) error

	// FindByAccount 通过账号查找用户（支持account或self_account）
	FindByAccount(account string) (*model.User, error)
}

type userRepository struct {
	db *gorm.DB
}

func NewUserRepository(db *gorm.DB) UserRepository {
	return &userRepository{db: db}
}

func (r *userRepository) Create(entity *model.User) (*model.User, error) {
	err := r.db.Model(&model.User{}).Create(entity).Error
	return entity, err
}

func (r *userRepository) FindByID(id uint) (*model.User, error) {
	var entity model.User
	err := r.db.First(&entity, id).Error
	return &entity, err
}

func (r *userRepository) FindAll(params *request.UserQueryRequest) ([]*model.User, int64, error) {
	var entities []*model.User
	var total int64

	query := r.db.Model(&model.User{})

	if params != nil {
		// 应用查询条件
		if params.UserName != nil {
			query = query.Where("user_name LIKE ?", "%"+*params.UserName+"%")
		}
		if params.Sex != nil {
			query = query.Where("sex LIKE ?", "%"+*params.Sex+"%")
		}
		if params.Sex != nil {
			query = query.Where("sex = ?", *params.Sex)
		}
		if params.Age != nil {
			query = query.Where("age = ?", *params.Age)
		}
		if params.Account != nil {
			query = query.Where("account LIKE ?", "%"+*params.Account+"%")
		}
		if params.SelfAccount != nil {
			query = query.Where("self_account LIKE ?", "%"+*params.SelfAccount+"%")
		}
		if params.UserRole != nil {
			query = query.Where("user_role LIKE ?", "%"+*params.UserRole+"%")
		}
		if params.UserRole != nil {
			query = query.Where("user_role = ?", *params.UserRole)
		}
		if params.Password != nil {
			query = query.Where("password LIKE ?", "%"+*params.Password+"%")
		}
		if params.UserStatus != nil {
			query = query.Where("user_status LIKE ?", "%"+*params.UserStatus+"%")
		}
		if params.UserStatus != nil {
			query = query.Where("user_status = ?", *params.UserStatus)
		}

	}

	query.Count(&total)
	query.Order("create_time desc")
	err := query.Offset((params.Current - 1) * params.PageSize).Limit(params.PageSize).Find(&entities).Error
	return entities, total, err
}

func (r *userRepository) Update(id uint, entity *request.UserUpdateRequest) error {
	return r.db.Model(&model.User{}).Where("id = ?", id).Updates(entity).Error
}

func (r *userRepository) Delete(id uint) error {
	return r.db.Delete(&model.User{}, id).Error
}

func (r *userRepository) Restore(id uint) error {
	return r.db.Unscoped().Model(&model.User{}).Where("id = ?", id).Update("delete_time", nil).Error
}

func (r *userRepository) ForceDelete(id uint) error {
	return r.db.Unscoped().Delete(&model.User{}, id).Error
}

// FindByAccount 通过账号查找用户（支持account或self_account）
func (r *userRepository) FindByAccount(account string) (*model.User, error) {
	var user model.User
	// 使用OR条件查询，同时匹配account或self_account
	err := r.db.Where("account = ? OR self_account = ?", account, account).First(&user).Error
	if err != nil {
		return nil, err
	}
	return &user, nil
}
